package com.fan.teamer.service;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.transaction.annotation.Transactional;

import com.fan.teamer.model.dao.AttendanceDao;
import com.fan.teamer.model.dao.EventDao;
import com.fan.teamer.model.dao.MemberDao;
import com.fan.teamer.model.dao.MessageDao;
import com.fan.teamer.model.dao.RoleDao;
import com.fan.teamer.model.dao.TeamDao;
import com.fan.teamer.model.dao.TeamMemberDao;
import com.fan.teamer.model.domain.Attendance;
import com.fan.teamer.model.domain.Event;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Message;
import com.fan.teamer.model.domain.Team;

@Named("memberService")
@Transactional(readOnly = true)
public class MemberServiceImpl implements MemberService {

	@Inject
	@Named("memberDao")
	private MemberDao memberDao;

	@Inject
	@Named("teamMemberDao")
	private TeamMemberDao teamMemberDao;

	@Inject
	@Named("teamDao")
	private TeamDao teamDao;

	@Inject
	@Named("roleDao")
	private RoleDao roleDao;

	@Inject
	@Named("attendanceDao")
	private AttendanceDao attendanceDao;

	@Inject
	@Named("eventDao")
	private EventDao eventDao;

	@Inject
	@Named("messageDao")
	private MessageDao messageDao;

	public MemberDao getMemberDao() {
		return memberDao;
	}

	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}

	public TeamMemberDao getTeamMemberDao() {
		return teamMemberDao;
	}

	public void setTeamMemberDao(TeamMemberDao teamMemberDao) {
		this.teamMemberDao = teamMemberDao;
	}

	public TeamDao getTeamDao() {
		return teamDao;
	}

	public void setTeamDao(TeamDao teamDao) {
		this.teamDao = teamDao;
	}

	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public AttendanceDao getAttendanceDao() {
		return attendanceDao;
	}

	public void setAttendanceDao(AttendanceDao attendanceDao) {
		this.attendanceDao = attendanceDao;
	}

	public EventDao getEventDao() {
		return eventDao;
	}

	public void setEventDao(EventDao eventDao) {
		this.eventDao = eventDao;
	}

	public MessageDao getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getList() {
		return memberDao.getAllMembers();
	}

	@Override
	public int getColumnsSize() {
		return memberDao.getColumnNames().size();
	}

	@Override
	public Object copy(Object obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Member getMember(String id) {
		return memberDao.getMember(id);
	}

	@Override
	public List<Member> getAllMembers() {
		return memberDao.getAllMembers();
	}

	@Override
	public int getSize() {
		return memberDao.getSize();
	}

	@Override
	@Transactional(readOnly = false)
	public void addMember(Member member) {
		memberDao.addMember(member);
	}

	@Override
	@Transactional(readOnly = false)
	public void updateStock(Member member) {
		memberDao.updateStock(member);
	}

	@Override
	@Transactional(readOnly = false)
	public void deleteMember(Member member) {
		memberDao.deleteMember(member);
	}

	@Override
	public boolean hasMember(String email) {
		return memberDao.hasMember(email);
	}

	@Override
	public List<String> getColumnNames() {
		return memberDao.getColumnNames();
	}

	@Override
	public List<Team> getJoinedTeams(Member member) {
		List<Team> teams = new ArrayList<Team>();
		for (int teamId : teamMemberDao.getTeamsIdListByMember(member)) {
			teams.add(teamDao.getTeam(teamId));
		}
		return teams;
	}

	@Override
	public String getUserGroup(String email) {

		return roleDao.getRole(email).getRoleGroup();
	}

	@Override
	public String getMemberRoleInTeam(Member member, Team team) {

		return teamMemberDao.getMemberRoleInTeam(member, team);
	}

	@Override
	public List<Event> getEventsByMember(Member member) {
		List<Attendance> attendances = attendanceDao
				.getMemberAttendance(member);
		List<Event> events = new ArrayList<Event>();
		for (Attendance attendance : attendances) {
			int eventId = attendance.getEventId();

			events.add(eventDao.getEvent(eventId));
		}
		return events;
	}

	@Override
	public List<Message> getReceivedMessages(Member member) {
		return messageDao.getMessagesByReceiver(member);
	}

	@Override
	public List<Message> getSentMessages(Member member) {
		return messageDao.getMessagesBySender(member);
	}

	@Override
	public Member getCurrentLoginMember() {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		UserDetails userDetails = null;
		if (principal instanceof UserDetails) {
		  userDetails = (UserDetails) principal;
		}
		if(userDetails == null)
		{
			return null;
		}
		String userName = userDetails.getUsername();
		Member member = memberDao.getMember(userName);
		
		return member;
	}

}
